import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class Reader {
	
	public  ArrayList<Entity>[] readFile() throws IOException{
        JOptionPane.showMessageDialog(null,"Please select the data to be matched");
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.setMultiSelectionEnabled(false);
        fileChooser.setCurrentDirectory(new File("D:\\School\\"));
        int returnVal = fileChooser.showOpenDialog(null);
        ArrayList<Entity>[] data;

        if (returnVal == JFileChooser.APPROVE_OPTION){
        	File file = fileChooser.getSelectedFile();
        	data = read(file);
        	return data;
        } 
        else if (returnVal == JFileChooser.CANCEL_OPTION || returnVal == JFileChooser.ERROR_OPTION) {
        	System.exit(0);
        }
        return null;
    }

	public ArrayList<Entity>[] read(File fileName) throws IOException{
		FileReader reader = new FileReader(fileName);
		BufferedReader input = new BufferedReader(reader);
		String line;
		String firstList = null;
		ArrayList[] result = new ArrayList[2];
		ArrayList<Entity> list1 = new ArrayList<Entity>();
		ArrayList<Entity> list2 = new ArrayList<Entity>();
		
		while((line = input.readLine()) != null){
			String[] split;
			split = line.split("\\t");
			if(firstList == null){
				firstList = split[0];
			}
			if(split.length > 2 && split[0].equals(firstList)){
				Entity temp = new Entity(split[0], split[1], split[2]);
//				System.out.println(temp);
				list1.add(temp);
			}
			else if(split.length > 2 && !split[0].equals(firstList)){
				Entity temp = new Entity(split[0], split[1], split[2]);
//				System.out.println(temp);
				list2.add(temp);
			}
			else{
				String debugInfo = Integer.toString(split.length);
				for(String part : split){
					debugInfo += ", " + part;
				}
				System.out.println(debugInfo);
			}
		}
//		System.out.println(list1.size());
//		System.out.println(list2.size());
		result[0] = list1;
		result[1] = list2;
		reader.close();
		return result;
	}
}
